﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;

namespace PeisongWeb
{
    public partial class buyer_budgetEdit : customerPage
    {
        protected int bid
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["bid"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["bid"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["bid"] != null)
                {
                    if (Library.Number.IsIntPositive(Request.QueryString["bid"]))
                    {
                        bid = Convert.ToInt32(Request.QueryString["bid"]);
                    }
                }
                drpYearBind();
                if (bid != 0)
                {
                    if (isShow(bid))
                    {
                        getDetails();
                    }
                    else
                    {
                        bid = 0;
                    }
                }
            }
        }

        private bool isShow(int bid)
        {
            BuyBudgetEntity budgetEnt = logic.buyBudget.getById(bid);
            int inputCmpId = logic.buyBudget.getById(bid).CompanyId;
            int inputYear = logic.buyBudget.getById(bid).Byear;
            int inputMonth = logic.buyBudget.getById(bid).Bmonth;
            bool isShow = inputCmpId == logic.customer.getCompanyId() ? (inputYear == DateTime.Now.Year ? (inputMonth <= DateTime.Now.Month ? false : true) : true) : false;
            return isShow;
        }

        private void drpYearBind()
        {
            int byear = DateTime.Now.Year;
            for (int i = byear; i <= byear + 5; i++)
            {
                ddl_Byear.Items.Add(i.ToString());
            }
            drpMonthBind();
        }
        private void drpMonthBind()
        {
            int byear = DateTime.Now.Year;
            int bmonth = DateTime.Now.Month;
            int selYear=Convert.ToInt32(ddl_Byear.SelectedValue);
            if (ddl_Byear.SelectedValue != "")
            {
                ddl_Bmonth.Items.Clear();
                if ( selYear!= byear)
                {
                    bmonth = 1;
                }
                else
                {
                    for (int i = 0; i < ddl_Bmonth.Items.Count; i++)
                    {

                        if (ddl_Bmonth.Items[i].Value == bmonth.ToString())
                        {
                            ddl_Bmonth.Items[i].Selected = true;
                        }
                    }
                }
            }
            for (int i = bmonth; i <= 12; i++)
            {
                if (!logic.buyBudget.isExistDate(selYear, i,logic.customer.getCompanyId()))
                {
                    ddl_Bmonth.Items.Add(i.ToString());
                }
            }
        }

        private void getDetails()
        {
            BuyBudgetEntity budgetEnt = logic.buyBudget.getById(bid);
            ddl_Byear.SelectedValue = budgetEnt.Byear.ToString();
            ddl_Bmonth.SelectedValue = budgetEnt.Bmonth.ToString();
            lbl_Year.Text = budgetEnt.Byear.ToString();
            lbl_Month.Text = budgetEnt.Bmonth.ToString();
            txt_BudgetFee.Value = string.Format("{0:F2}", budgetEnt.BudgetFee);
            hd_isHint.Value = budgetEnt.IsHint.ToString();
            li_Date1.Visible = false;
            li_Date2.Visible = true;
        }
        protected void ddl_Byear_SelectedIndexChanged(object sender, EventArgs e)
        {
            drpMonthBind();
        }
        protected void lbtn_Save_Click(object sender, EventArgs e)
        {
            BuyBudgetEntity budgetEnt = new BuyBudgetEntity();
            budgetEnt.CompanyId = logic.customer.getCompanyId();
            budgetEnt.BudgetFee = Convert.ToDecimal(txt_BudgetFee.Value);
            budgetEnt.Byear = Convert.ToInt32(ddl_Byear.SelectedValue);
            budgetEnt.Bmonth = Convert.ToInt32(ddl_Bmonth.SelectedValue);
            if (bid != 0)
            {
                budgetEnt.IsHint = Convert.ToBoolean(hd_isHint.Value);
                budgetEnt.Bid = bid;
                logic.buyBudget.update(budgetEnt);
            }
            else
            {
                budgetEnt.IsHint = true;
                logic.buyBudget.insert(budgetEnt);
            }
            Library.Script.ClientMsgUrl("保存成功！", "buyer_budgetList.html");
        }
    }
}
